package com.jeeplus.test.jlnu.archivalInfo.utils;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;
import java.util.zip.*;

@Component
public class ZipUtils {

    // 创建 Excel 文件
    public static void createExcel(String filePath, String sheetName) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet(sheetName);
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("测试数据");

            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
        }
    }

    // 递归压缩文件夹
    public static void zipFolder(String folderPath, String zipFilePath, Map<String, List<String>> folderToFiles) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zipOut = new ZipOutputStream(fos)) {
            File folder = new File(folderPath);
            zipFile(folder, folder.getName(), zipOut, folderToFiles);
            zipOut.finish();
        }
    }


    private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut, Map<String, List<String>> folderToFiles) throws IOException {
        if (fileToZip.isHidden()) {
            return;
        }

        if (fileToZip.isDirectory()) {
            // 递归添加物理文件
            for (File childFile : Objects.requireNonNull(fileToZip.listFiles())) {
                zipFile(childFile, fileName + "/" + childFile.getName(), zipOut, folderToFiles);
            }

            // ✅ 处理 MinIO 下载的文件，跳过已经被递归添加的文件
            List<String> extraFiles = folderToFiles.get(fileToZip.getAbsolutePath().replace("\\", "/"));
            if (extraFiles != null) {
                for (String filePath : extraFiles) {
                    File extraFile = new File(filePath);
                    if (extraFile.exists()) {
                        // **跳过已经在物理目录中的文件**
                        if (!extraFile.getAbsolutePath().startsWith(fileToZip.getAbsolutePath())) {
                            System.out.println("正在添加 MinIO 文件：" + extraFile.getAbsolutePath());
                            zipSingleFile(extraFile, fileName + "/" + extraFile.getName(), zipOut);
                        }
                    } else {
                        System.out.println("⚠️ MinIO 文件未找到：" + filePath);
                    }
                }
            }
        } else {
            zipSingleFile(fileToZip, fileName, zipOut);
        }
    }
    // 处理单个文件的压缩
    private static void zipSingleFile(File file, String zipEntryName, ZipOutputStream zipOut) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            ZipEntry zipEntry = new ZipEntry(zipEntryName);
            zipOut.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
            zipOut.closeEntry();
        }
    }

    // 递归删除目录
    public static void deleteDirectory(File directory) {
        if (directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                deleteDirectory(file);
            }
        }
        directory.delete();
    }

}
